//
//  ChooselabView.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/24.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChooselabView.h"
#import "ChooselabCell.h"
@interface ChooselabView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSString  *typeStr;//类型
@property (nonatomic,strong)NSMutableArray  *chooseArr;//已经选中的
@property (nonatomic, strong)NSArray *firstArr;//主数据
@property (nonatomic, strong)NSArray *secondArr;//备用 万一用到了呢
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, assign) NSInteger selectRow;//选中的Row
@property (nonatomic,strong)UIButton *saveBtn;//确定按钮
@end

@implementation ChooselabView

- (void)WithFrame:(CGRect)frame KindStr:(NSString *)kindStr DataArr:(NSArray *)dataArr SecondArr:(NSArray *)dataArr2 ChooseArr:(NSMutableArray *)chooseArr
{
    self.backgroundColor = [UIColor whiteColor];
    self.frame = frame;
    _firstArr = dataArr;
    _secondArr = dataArr2;
    _typeStr = kindStr;
    _selectRow = 0;
    _chooseArr = [[NSMutableArray alloc]initWithArray:chooseArr];
    [self addSubview:self.table];
}
-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) style:UITableViewStyleGrouped];
        _table.estimatedRowHeight = 80*screenW_6;
        _table.delegate = self;
        _table.dataSource = self;
        _table.showsHorizontalScrollIndicator = NO;
        _table.showsVerticalScrollIndicator = NO;
        _table.separatorStyle = 0;
        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _table.backgroundColor = [UIColor whiteColor];
    }
    return _table;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45*screenW_6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == _firstArr.count -1) {
        return 80*screenW_6;
    }
    return 0.000001;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _firstArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载Cell
    ChooselabCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"ChooselabCell%ld",indexPath.row]];
    if (cell == nil) {
        cell = [[ChooselabCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"ChooselabCell%ld",indexPath.row]];
    }
    [cell loadCellArr:_firstArr[indexPath.section][1] Choose:_chooseArr];
    cell.chooseCellblock = ^(NSString *title,BOOL isBlock){
        
        if (isBlock) {//选择回调
            [_chooseArr addObject:title];
            if (_chooseRowBlock) {
                self.chooseRowBlock(_chooseArr,title);
            }
        }else//删除已经选中的title
        {
            [_chooseArr removeObject:title];
        }
       
    };
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //区头类别展示
    UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 45*screenW_6)];
    UILabel *titleLab = [TomamaUI createLabel:CGRectMake(15*screenW_6, 0, SCREENW-30*screenW_6, 45*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:TextBlack text:_firstArr[section][0]];
    [headerV addSubview:titleLab];
    return headerV;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    //区尾按钮
    if (section == _firstArr.count-1) {
        UIView *footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 80*screenW_6)];
        [footV addSubview:self.saveBtn];
        return footV;
    }
    return nil;
}
-(UIButton *)saveBtn//确定按钮
{
    if (!_saveBtn) {
        _saveBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(15*screenW_6,20*screenW_6, SCREENW-30*screenW_6, 45*screenW_6) targ:self sel:@selector(saveSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"确定" image:@"" backGroundImage:@""];
        _saveBtn.backgroundColor = [UIColor colorWithHexString:@"#59cbb3"];
        [_saveBtn.layer setCornerRadius:5*screenW_6];
        _saveBtn.layer.masksToBounds = YES;
    }
    return _saveBtn;
}

-(void)saveSelectBtn//确定点击事件
{
    if (_chooseRowBlock) {
        self.chooseRowBlock(0,@"确定");
    }
}
@end

